﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CDefineGlobal;
using CObject;

namespace BUS_LAYER
{
    public class BUS_Rocket
    {
        public static void CheckCollisions(AbstractRocket rocket, CMap map)
        {
            int xCor = rocket.Location.X;
            int yCor = rocket.Location.Y;
            int xNewCor = xCor;
            int yNewCor = yCor;

            // wants to move right, check if possible yet
            if (rocket.I_ToMove == CDefine.DIRECTION_RIGHT)
            {
                xNewCor += rocket.I_Speed;

                if (BUS_Utility.CanMove(xNewCor, yNewCor, map))
                {
                    rocket.I_XMove = rocket.I_Speed;
                    rocket.I_YMove = 0;
                }
                else
                {
                    rocket.I_XMove = 0;
                    rocket.I_YMove = 0;
                    rocket.I_ToMove = CDefine.DIRECTION_NONE;
                }
            } // end if right

            // wants to move left, check if possible yet
            else if (rocket.I_ToMove == CDefine.DIRECTION_LEFT)
            {
                xNewCor -= rocket.I_Speed;

                if (BUS_Utility.CanMove(xNewCor, yNewCor, map))
                {
                    rocket.I_XMove = -rocket.I_Speed;
                    rocket.I_YMove = 0;
                }
                else
                {
                    rocket.I_XMove = 0;
                    rocket.I_YMove = 0;
                    rocket.I_ToMove = CDefine.DIRECTION_NONE;
                }
            } // end if left

            // wants to move down, check if possible yet
            else if (rocket.I_ToMove == CDefine.DIRECTION_DOWN)
            {
                yNewCor += rocket.I_Speed;

                if (BUS_Utility.CanMove(xNewCor, yNewCor, map))
                {
                    rocket.I_XMove = 0;
                    rocket.I_YMove = rocket.I_Speed;
                }
                else
                {
                    rocket.I_XMove = 0;
                    rocket.I_YMove = 0;
                    rocket.I_ToMove = CDefine.DIRECTION_NONE;
                }
            } // end if down

            // wants to move up, check if possible yet
            else if (rocket.I_ToMove == CDefine.DIRECTION_UP)
            {
                yNewCor -= rocket.I_Speed;
                //yNewCor -= 10;  //10 là kích thước của viên đạn

                if (BUS_Utility.CanMove(xNewCor, yNewCor, map))
                {
                    rocket.I_XMove = 0;
                    rocket.I_YMove = -rocket.I_Speed;
                }
                else
                {
                    rocket.I_XMove = 0;
                    rocket.I_YMove = 0;
                    rocket.I_ToMove = CDefine.DIRECTION_NONE;
                }
            } // end if up

            xCor += rocket.I_XMove;
            yCor += rocket.I_YMove;

            if (xCor > CDefine.GAME_WIDTH || xCor < CDefine.XTOP_LEFT || yCor > CDefine.GAME_HEIGHT || yCor < CDefine.YTOP_LEFT)
                rocket.Enabled = false;

            if (rocket.I_XMove == 0 && rocket.I_YMove == 0)
            {
                rocket.I_ToMove = CDefine.DIRECTION_NONE;

                //nổ gạch
                BUS_Map.CheckExplosionBrick(xNewCor, yNewCor, map);

                //đạn biến mất
                rocket.Enabled = false;
            }
            else if (BUS_Utility.CanMove(xCor, yCor, map))
                rocket.ChangeLocation(xCor, yCor);
        }
    }
}
